﻿package igz.fleaxy.fx {
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import igz.fleaxy.util.ImageUtil;
	import igz.fleaxy.util.ObjectUtil;
	import com.greensock.TweenNano;

	/**
	 * @author Intelygenz | www.intelygenz.com
	 * 
	 * Introduce un "fade" haciendo un efecto de desaparición de un elemento
	 */
	public class FxGrowFade {
		
		/**
		 * Íntroduce en el elemento un efecto de blur y desaparición
		 * @param	p_displayObject Objeto al que se le realiza la transformacións
		 * @param	p_settings _Settings : Object = {
								"duration" :2 //segundos
								,"scaleX" :1.5
								,"scaleY" :1.5
								,"onComplete": funcion que se ejecuta al terminar
							};
		 */
		static public function $Start(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			new FxGrowFade()._StartGrow(p_displayObject, p_settings);
		}


		protected var _Settings:Object = { duration : 0.5
										 , scaleX : 2
										 , scaleY : 2
										 , onComplete : null
										 };
		private var _Tween : TweenNano;
		
		public function FxGrowFade() {

		}

		protected function _StartGrow(p_displayObject:DisplayObject, p_settings:Object = null) : void {
			ObjectUtil.$Merge(p_settings, _Settings);

			var bmp:Bitmap = ImageUtil.$GetBitmap(p_displayObject);
			bmp.x = p_displayObject.x;
			bmp.y = p_displayObject.y;
			p_displayObject.parent.addChild(bmp);

			var xEnd:Number = bmp.x - ((bmp.width*_Settings["scaleX"])/2) + (bmp.width/2);
			var yEnd:Number = bmp.y - ((bmp.height * _Settings["scaleY"]) / 2) + (bmp.height / 2);

			_Tween = TweenNano.to(bmp, _Settings["duration"], { x : xEnd, y : yEnd
										 , scaleX : _Settings["scaleX"], scaleY : _Settings["scaleY"]
										 , alpha : 0
										 , onComplete : _Grow_OnComplete, onCompleteParams : [bmp] 
										 } 
						);
		}

		protected function _Grow_OnComplete(p_bmp:Bitmap) : void {
			_Tween.kill();
			p_bmp.parent.removeChild(p_bmp);
			p_bmp = null;

			if ( _Settings["onComplete"] != null ) {
				_Settings["onComplete"]();
			}
		}

	}

}